package com.lly.songhigh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lly.songhigh.R;
import com.lly.songhigh.activity.SingerDetailActivity;
import com.lly.songhigh.adapter.SingerTopAdapter;
import com.lly.songhigh.constant.StringResouce;
import com.lly.songhigh.constant.URL;
import com.lly.songhigh.entity.SingerTop;
import com.lly.songhigh.volley.VolleySingleton;

import java.util.List;

/**
 * Created by 李新伟 on 15/11/4.
 */
public class SingerClassifyFragment extends BaseFragment implements AdapterView.OnItemClickListener {

    private String url;
    private ListView listView;
    private SingerTopAdapter adapter;
    private List<SingerTop.DataEntity> data;
    private VolleySingleton volleySingleton;
    private TextView singerClassify;

    public static SingerClassifyFragment newInstance(String url) {
        Bundle args = new Bundle();
        args.putString("url", url);
        SingerClassifyFragment fragment = new SingerClassifyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_male, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        listView = (ListView) view.findViewById(R.id.man_singer_listView);
        singerClassify = (TextView) view.findViewById(R.id.singerClassify);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        volleySingleton = VolleySingleton.getInstance();
        url = getArguments().getString("url");
        if (url.equals(URL.SINGER_TOP)) {
            singerClassify.setVisibility(View.VISIBLE);
            singerClassify.setText(StringResouce.TOP100);
        }
        if (url.equals(URL.RECENTLY_HOT)) {
            singerClassify.setVisibility(View.VISIBLE);
            singerClassify.setText(StringResouce.MOVE_UP_QUICK);
        }
        initDatas();
    }

    private void initDatas() {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {
                    Gson gson = new Gson();
                    SingerTop result = gson.fromJson(response, SingerTop.class);
                    if (result != null && result.getData() != null) {
                        data = result.getData();
                        adapter = new SingerTopAdapter(getActivity(), data);
                        listView.setAdapter(adapter);
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        volleySingleton.addQueue(request);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SingerTop.DataEntity data = (SingerTop.DataEntity) parent.getItemAtPosition(position);
        Intent intent = new Intent(getActivity(), SingerDetailActivity.class);
        intent.putExtra("singerId", data.getSinger_id());
        startActivity(intent);
    }
}
